// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.csv;

import static org.dengues.core.IDesignerCoreService.CSV_HEADER;
import static org.dengues.core.IDesignerCoreService.CSV_SEPARATOR;

import java.util.HashMap;
import java.util.List;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.viewers.wizards.NewElementWizardPage3;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-7-4 qiang.zhang $
 * 
 */
public class NewCSVWizardPage3 extends NewElementWizardPage3 {

    private Text seqtext;

    private Text headertext;

    private Text counttext;

    /**
     * Qiang.Zhang.Adolf@gmail.com NewCSVWizardPage3 constructor comment.
     * 
     * @param path
     * @param database
     */
    public NewCSVWizardPage3(IPath path, DatabaseDiagram database, ENodeCategoryName name) {
        super(path, database, name);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage3#createMiddleContent(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createMiddleContent(Composite container) {
        Composite composite = new Composite(container, SWT.NONE);
        GridLayout gridLayout = new GridLayout(6, false);
        composite.setLayout(gridLayout);
        Label label = new Label(composite, SWT.NONE);
        label.setText(Messages.getString("NewCSVWizardPage3.seq")); //$NON-NLS-1$
        seqtext = new Text(composite, SWT.BORDER);
        GridData gridData = new GridData();
        gridData.widthHint = 120;
        seqtext.setLayoutData(gridData);
        if (database != null) {
            seqtext.setText((String) database.getProperties().get(CSV_SEPARATOR));
        } else {
            seqtext.setText(";");
        }

        label = new Label(composite, SWT.NONE);
        label.setText(Messages.getString("NewCSVWizardPage3.header")); //$NON-NLS-1$
        headertext = new Text(composite, SWT.BORDER);
        gridData = new GridData();
        gridData.widthHint = 120;
        headertext.setLayoutData(gridData);
        if (database != null) {
            headertext.setText((String) database.getProperties().get(CSV_HEADER));
        } else {
            headertext.setText("0");
        }

        label = new Label(composite, SWT.NONE);
        label.setText("Column Count:");
        counttext = new Text(composite, SWT.BORDER);
        gridData = new GridData();
        gridData.widthHint = 120;
        counttext.setLayoutData(gridData);
        counttext.setText("20");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage3#getProperties()
     */
    @Override
    public HashMap<String, Object> getProperties() {
        HashMap<String, Object> properties = super.getProperties();
        properties.put(CSV_SEPARATOR, seqtext.getText());
        properties.put(CSV_HEADER, headertext.getText());
        return properties;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage3#addListeners()
     */
    @Override
    protected void addListeners() {
        guessBtn.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            public void widgetSelected(SelectionEvent e) {
                List<String[]> csvRows = DenguesCorePlugin.getDefault().getDesignerCoreService().getCVSPreviewRows(getFileName(),
                        seqtext.getText(), headertext.getText(), enCodingtext.getText());
                retieveSchema(csvRows, null);
            }
        });
    }
}
